package com.lz.base.project.thread.thread;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.StopWatch;
import com.lz.base.common.utils.http.OkHttpUtil;
import com.lz.base.common.utils.http.SstInf;
import com.lz.base.project.thread.thread.entity.DemoRes;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CountDownLatch;

public class ThreadLz {

    public static void main(String[] args) {
        t1();
        t2();
    }

    public static void t1() {
        Map<String, String> map = new HashMap<>();
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    SstInf sstInf = new SstInf();
                    sstInf.setInfFullUrl("https://query.asilu.com/weather/baidu");
                    sstInf.setInfRequest("{city: 济宁}");
                    String s = OkHttpUtil.requestGet(sstInf);
                    System.out.println("济宁:" + s);
                    map.put("济宁", s);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });
        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    SstInf sstInf = new SstInf();
                    sstInf.setInfFullUrl("https://query.asilu.com/weather/baidu");
                    sstInf.setInfRequest("{city: 砀山}");
                    String s = OkHttpUtil.requestGet(sstInf);
                    System.out.println("砀山:" + s);
                    map.put("砀山", s);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });

        Thread t3 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    SstInf sstInf = new SstInf();
                    sstInf.setInfFullUrl("https://query.asilu.com/weather/baidu");
                    sstInf.setInfRequest("{city: 杭州}");
                    String s = OkHttpUtil.requestGet(sstInf);
                    System.out.println("杭州:" + s);
                    map.put("杭州", s);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });

        Thread t4 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    SstInf sstInf = new SstInf();
                    sstInf.setInfFullUrl("https://query.asilu.com/weather/baidu");
                    sstInf.setInfRequest("{city: 上海}");
                    String s = OkHttpUtil.requestGet(sstInf);
                    System.out.println("上海:" + s);
                    map.put("上海", s);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });

        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        t1.start();
        t2.start();
        t3.start();
        t4.start();
        try {
            t1.join();
            t2.join();
            t3.join();
            t4.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("map:" + Convert.toStr(map));
        stopWatch.stop();
        System.out.println("总耗时：" + stopWatch.getTotalTimeMillis());
    }

    public static void t2()  {
        System.out.println(System.currentTimeMillis());

        CountDownLatch countDownLatch = new CountDownLatch(2);

        //用于存放子线程执行的结果
        DemoRes.Result<Set> result = new DemoRes.Result<>();
        Set set = new HashSet();
        result.setResult(set);
        //创建一个子线程
        Thread thread1 = new Thread(() -> {
            try {
                SstInf sstInf = new SstInf();
                sstInf.setInfFullUrl("https://query.asilu.com/weather/baidu");
                sstInf.setInfRequest("{city: 上海}");
                String s = OkHttpUtil.requestGet(sstInf);
                System.out.println("上海:" + s);
                set.add(s);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                countDownLatch.countDown();
            }
        });

        Thread thread2 = new Thread(() -> {
            try {
                SstInf sstInf = new SstInf();
                sstInf.setInfFullUrl("https://query.asilu.com/weather/baidu");
                sstInf.setInfRequest("{city: 北京}");
                String s = OkHttpUtil.requestGet(sstInf);
                System.out.println("北京:" + s);
                set.add(s);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                countDownLatch.countDown();
            }
        });

        thread1.start();
        thread2.start();
        //countDownLatch.await()会让当前线程阻塞，当countDownLatch中的计数器变为0的时候，await方法会返回
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        //获取thread线程的执行结果
        System.out.println(  "set:" + Convert.toStr(result.getResult()));

    }

}
